package com.adonis.ejoy;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.adonis.ejoy.Item.Comment;
import com.adonis.ejoy.adapter.CommentsAdapter;
import com.adonis.ejoy.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CommentActivity extends AppCompatActivity {

    @BindView(R.id.rvComments)
    RecyclerView comment_list;
    @BindView(R.id.etComment)
    EditText etComment;

    private int pid;
    private int uid;
    private String baseURL;
    private List<Comment> comments = new ArrayList<>();
    private CommentsAdapter adapter_comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);
        pid = getIntent().getIntExtra("pid",0);
        uid = getIntent().getIntExtra("uid",0);
        baseURL = getString(R.string.baseURL);
        initComment();
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);  //new一个线性布局管理器
        comment_list.setLayoutManager(layoutManager);
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    if(msg.obj instanceof List){
                        comments = (List<Comment>)msg.obj;
                        adapter_comment = new CommentsAdapter(comments);
                        comment_list.setAdapter(adapter_comment);
                    }
                    break;
                case 1:
                    comments.add((Comment)msg.obj);
                    adapter_comment.notifyDataSetChanged();
                    etComment.setText(null);
                    break;
            }
        }
    };

    private void initComment() {
        final String url = baseURL + "mvc/comment?&pid=" + pid;
        HttpUtil.sendGetRequest(url,new Callback() {             //发送网络请求不能在主线程中进行
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("onFailure: ", "failed");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call,final Response response) throws IOException {
                String jsonData = response.body().string();
                Gson gson = new Gson();
                comments = gson.fromJson(jsonData,new TypeToken<List<Comment>>(){}.getType());  //将JSON数组解析为comment集合
                Message message = new Message();
                message.obj = comments;
                message.what = 0;               //获取评论
                handler.sendMessage(message);
                Log.d("commentList", comments.toString());
            }
        });
    }

    @OnClick({R.id.btn_back,R.id.btnSendComment})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.btnSendComment:
                sendComment();
                break;
        }
    }

    private void sendComment() {
        final String comment = etComment.getText().toString();
        String url = baseURL + "mvc/comment";
        RequestBody body = new FormBody.Builder()
                .add("uid", uid + "")
                .add("pid", pid + "")
                .add("comment",comment)
                .build();
        HttpUtil.sendPostRequest(url, body, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String img = response.body().string();
                Comment cm = new Comment();
                cm.setName("张三");
                cm.setComment(comment);
                cm.setImg(img);
                Message message = new Message();
                message.what = 1;              //发送评论
                message.obj = cm;
                handler.sendMessage(message);
            }
        });
    }
}
